/**
 * 
 */

$(document).ready(function() {
	
	var leftIframe = "type.jsp?keyWord= ";
	var rightIframe = "shop.jsp?type=0&keyWord= ";

	var str = window.location.search;
	if(str != null && str != ""){
		var strs = str.split("&");
		if(strs.length == 2){
			rightIframe = strs[0].split("=")[1];
			leftIframe = strs[1].split("=")[1];
		}else{
			rightIframe = str.split("=")[1];
		}
	}
	$("#leftIframe").attr("src",leftIframe);
	$("#rightIframe").attr("src",rightIframe);
	
	//进入会员中心
	$('#vip').click(function(){
		var id = $(".handler").attr("id");
		if(id == "" || id == null){
			alert("请先登录账号");
			return ;
		}else {
			$("#leftIframe").attr("src","vip.jsp?id="+id);
			$("#rightIframe").attr("src","viewInfo.jsp?id="+id);
		}
	});	
	//进入购物车
	$('#car').click(function(){
		var id = $(".handler").attr("id");
		if(id == "" || id == null){
			alert("请先登录账号");
			return ;
		}else {
			rightIframe = "car?id="+id;
			$("#rightIframe").attr("src",rightIframe);
		}
	});	
	//进入我的订单
	$('#myOrder').click(function(){
		var id = $(".handler").attr("id");
		if(id == "" || id == null)
			alert("请先登录");
		else
			$("#rightIframe").attr("src","initMyOrder?id="+id);
	});		
	//进入首页
	$('#index').click(function(){
		window.location.reload(true);
	});	
	//
	var littleTypeId = "";
	var bigTypeId = "";
	//小类别查询
	$("body").on("change","#littleType",function(){
		littleTypeId = $('#littleType  option:selected').attr("id");
		var keyWord = $("#keyword").val();
		if(keyWord == "" || keyWord == null){
			keyWord = " ";
		}
		if(littleTypeId == "" || littleTypeId == null){
			littleTypeId = $('select  option:selected').attr("id");
		}
		//修改传递给iframe的参数值
		rightIframe = "shop.jsp?type="+littleTypeId+"&keyWord="+keyWord;
		//leftIframe = "type.jsp?keyWord="+keyWord;
		$.get(
			"index/littleType",
			{"littleType":littleTypeId,"keyWord":keyWord},
			function(result){
				$('#rightIframe').attr('src', rightIframe);
			}
		)
	});
	//类别的级联效果
	$('#bigType').change(function(){
		bigTypeId = $('select  option:selected').attr("id");
		var keyWord = $("#keyword").val();
		if(keyWord == "" || keyWord == null){
			keyWord = " ";
		}
		if(bigTypeId == "" || bigTypeId == null){
			$("#littleType").empty(); 
			$("#littleType").prepend("<option>&nbsp&nbsp&nbsp&nbsp---&nbsp&nbsp&nbsp&nbsp</option>");
			bigTypeId = "0";
		}

		//修改传递给iframe的参数值
		rightIframe = "shop.jsp?type="+bigTypeId+"&keyWord="+keyWord;
		$.get(
			"index/bigType",
			{"bigType":bigTypeId,"keyWord":keyWord},
			function(result){
				$("#littleType").empty(); 
				$("#littleType").append("<option>&nbsp&nbsp&nbsp&nbsp---&nbsp&nbsp&nbsp&nbsp</option>");
				for(var i=0; i<result.length; i++){
					$("#littleType").append("<option id="+ result[i].id + " class='littleType' onchange='littleType()'>"+ result[i].name +"</option>");
				}
				$('#rightIframe').attr('src', rightIframe);
			}
		)
	});	

	////关键字搜索(内容改变后搜索)
	$('#keyword').change(function(){
		var type = "0";
		if(bigTypeId != "" && bigTypeId != null){
			type = bigTypeId;
		}
		if(littleTypeId != "" && littleTypeId != null){
			type = littleTypeId;
		}
		
		var keyWord = $("#keyword").val();
		if(keyWord == "" || keyWord == null){
			keyWord = " ";
		}
		//修改传递给iframe的参数值
		rightIframe = "shop.jsp?type="+type+"&keyWord="+keyWord;
		leftIframe = "type.jsp?keyWord="+keyWord;
		$.get(
			"index/search",
			{"type":type,"keyWord":keyWord},
			function(result){
				$('#rightIframe').attr('src', rightIframe);
				$('#leftIframe').attr('src', leftIframe);
			}
		)
	});		
	//注销用户
	$("#logout").click(function(){
		$.get(
			"index/logout",
			function(result){
				alert("注销成功！");
				window.location.reload(true);
			}
		)
	});
/*	//关键字搜索(点击图标搜索)
	$('#search').click(function(){
		var type = "0";
		if(bigTypeId != "" && bigTypeId != null){
			type = bigTypeId;
		}
		if(littleTypeId != "" && littleTypeId != null){
			type = littleTypeId;
		}
		
		var keyWord = $("#keyword").val();
		if(keyWord == "" || keyWord == null){
			keyWord = " ";
		}
		//修改传递给iframe的参数值
		rightIframe = "shop.jsp?type="+type+"&keyWord="+keyWord;
		leftIframe = "type.jsp?keyWord="+keyWord;
		$.get(
			"index/search",
			{"type":type,"keyWord":keyWord},
			function(result){
				$('#rightIframe').attr('src', rightIframe);
				$('#leftIframe').attr('src', leftIframe);
			}
		)
	});	*/	
})